package com.market.cash.ui.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.View
import com.market.cash.R
import com.market.cash.base.app.CashRootStackManager
import com.market.cash.pro.ProActivity
import com.market.cash.ui.fragment.PinFailedFragment
import com.market.cash.ui.fragment.PinProcessFragment
import com.market.cash.ui.fragment.PinSuccessFragment
import com.market.cash.ui.presenter.CashRootPinStatusPresenter
import com.market.cash.ui.view.ICashRootPinStatusView
import com.market.cash.utils.event.CashRootEventData
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/25 17:04
 * @Annotation：PIN 支付结果处理中
 */
class PinStatusActivity: ProActivity(),ICashRootPinStatusView {
    var presenter: CashRootPinStatusPresenter? = null

    companion object {
        private const val INTENT_KEY_PRODUCT = "product"
        private const val INTENT_KEY_TOKEN = "token"
        private const val INTENT_KEY_ORDER_ID = "thirdOrderId"
        fun start(context: Context, pName: String, pToken: String,orderId: String) {
            val intent = Intent(context, PinStatusActivity::class.java)
            intent.putExtra(INTENT_KEY_PRODUCT, pName)
            intent.putExtra(INTENT_KEY_TOKEN, pToken)
            intent.putExtra(INTENT_KEY_ORDER_ID, orderId)
            context.startActivity(intent)
        }
    }

    override fun getCashRootRootView(): View? {
        return null
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#F8F8F8"))

    }

    override fun initCashRootListener() {

    }

    override fun initCashRootData() {
        presenter = CashRootPinStatusPresenter(this)
        presenter!!.saveCashRootThirdOrderId(loadOrderId())
        presenter!!.loadCashRootPaymentStatusAction()
    }


    override fun loadPName(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras!!.getString(INTENT_KEY_PRODUCT).toString()
    }

    override fun loadPToken(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras!!.getString(INTENT_KEY_TOKEN).toString()
    }

    /**
     * 三方订单id
     */
    private fun loadOrderId(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras!!.getString(INTENT_KEY_ORDER_ID).toString()
    }

    /**
     * 支付成功
     */
    override fun showCashRootPaymentSuccess(data: JSONObject) {
        val paymentSuccess = PinSuccessFragment(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentSuccess)
    }

    /**
     * 支付失败
     */
    override fun showCashRootPaymentFailed(data: JSONObject) {
        val paymentFailedFm = PinFailedFragment(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentFailedFm)
    }

    /**
     * 支付处理中
     */
    override fun showCashRootPaymentProcess(data: JSONObject) {
        val paymentProcessFm = PinProcessFragment(this,data)
        replaceContainerFragement(R.id.containerLayout,paymentProcessFm)
    }

    /**
     * 支付成功 - 跳转到可申请列表
     */
    fun showCashRootProductsListAction(){
        //刷新产品列表
        publishEventData(CashRootEventData(CashRootEventData.TYPE.CASHROOT_PRODUCT_REFRESH))
        //返回首页
        publishEventData(CashRootEventData(CashRootEventData.TYPE.CASHROOT_SHOW_HOME))
        CashRootStackManager.finishAllActivity(MainActivity::class.java)
    }
}